﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Sale_Operation;
using Sale_Common;
using Sale_Model;
using SaleSystem;
using ExtAspNet;


namespace SaleSystem.Other
{
    public partial class frm_WorkPlanEdit : PageBase
    {
        #region Page Event
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                InitControl();
                BindDropDownList();
                GetAndBind();
                this.bnClose.OnClientClick = ExtAspNet.ActiveWindow.GetHideReference();
               
            }
        }

       public  void cb_WPlan_Enable_CheckedChanged(object sender, EventArgs e)
        {
            if (cb_WPlan_Enable.Checked)
            {
                this.ddl_WPlan_Number.Hidden = false;
                this.ddl_WPlan_Unit.Hidden = false;
            }
            else
            {
                this.ddl_WPlan_Number.Hidden = true;
                this.ddl_WPlan_Unit.Hidden = true;
            }


       }
        protected void bnSave_Click(object sender, EventArgs e)
        {
            SModel.CreateMan = LinkService.GetCurrentUser().SUser_Id;
            SModel.CreateTime = System.DateTime.Now;
            SModel.State = 1;
            SModel.WPlan_Content = this.tb_WPlan_Content.Text;
            SModel.WPlan_StartTime = DateTime.Parse(this.dp_WPlan_StartTime.Text);

            if (this.cb_WPlan_Enable.Checked)//需要提醒
            {
                SModel.WPlan_Enable = 1;
                SModel.WPlan_Number = ValueHandler.GetIntNumberValue(this.ddl_WPlan_Number.SelectedValue);
                SModel.WPlan_Unit = ValueHandler.GetIntNumberValue(this.ddl_WPlan_Unit.SelectedValue);
                if (this.ddl_WPlan_Number.SelectedValue == "-1" || this.ddl_WPlan_Unit.SelectedValue == "-1")
                {
                    ExtAspNet.Alert.ShowInTop("请选择提醒时间！", "操作提示！", MessageBoxIcon.Information);
                    return;
                }
                //构建提醒时间
                SModel.WPlan_WarningTime = GetWaringDate(SModel);
            }
            else
            {
                SModel.WPlan_Enable = 0;
                SModel.WPlan_Number=-1;
                SModel.WPlan_Unit=-1;
                SModel.WPlan_WarningTime = System.DateTime.MaxValue;
            }
            SModel.WPlan_EndTime = DateTime.Parse(this.dp_WPlan_EndTime.Text);
            SModel.WPlan_Id = this.Id;
            SModel.WPlan_Man = 1;
            if (this.cb_WPlan_State.Checked)
                SModel.WPlan_State = 1;
            else
                SModel.WPlan_State = 0;
            SModel.WPlan_Title = this.tb_WPlan_Title.Text;
            SModel.WPlan_Man = ValueHandler.GetIntNumberValue(this.ddl_WPlan_Man.SelectedValue);
        
            bool blRes = Manager.Save(SModel);
            if (blRes)
                ExtAspNet.PageContext.RegisterStartupScript(ExtAspNet.ActiveWindow.GetHidePostBackReference(LinkService.RefreshString));
            else
                ExtAspNet.Alert.Show("日程信息编辑失败！", MessageBoxIcon.Error);

        }
        #endregion

        #region Method
        private void InitControl()
        {
            bnClose.OnClientClick = ExtAspNet.ActiveWindow.GetHideReference();
            if (Request["Id"] != null)
            {
                Id = ValueHandler.GetIntNumberValue(Request["Id"]);
            }
        }
        private void BindDropDownList()
        {
            this.ddl_WPlan_Number.Items.Clear();
            //初始化数值
            for (int i = 0; i < 7; i++)
            {
                this.ddl_WPlan_Number.Items.Add(new ExtAspNet.ListItem((i+1).ToString(), (i+1).ToString()));
            }
            this.ddl_WPlan_Number.Items.Insert(0, new ExtAspNet.ListItem("请选择提醒时间", "-1"));
            DataTable dt = Manager.GetView_WorkPlanUnit();
            this.ddl_WPlan_Unit.DataSource = dt;
            this.ddl_WPlan_Unit.DataBind();
            this.ddl_WPlan_Unit.Items.Insert(0, new ExtAspNet.ListItem("请选择提醒时间", "-1"));

            SysUsersOp userManager = new SysUsersOp();
            DataTable dtUser = userManager.GetList("");
            this.ddl_WPlan_Man.DataSource = dtUser;
            this.ddl_WPlan_Man.DataBind();
            this.ddl_WPlan_Man.Items.Insert(0, new ExtAspNet.ListItem("-未选择-", "-1"));

            
        }
        private void GetAndBind()
        {
            if (Id != 0)
            {
                SModel.WPlan_Id = Id;
                SModel = Manager.GetModel(SModel) as WorkPlan;
                this.tb_WPlan_Content.Text = SModel.WPlan_Content;
                this.tb_WPlan_Title.Text = SModel.WPlan_Title;
                this.ddl_WPlan_Man.SelectedValue = SModel.WPlan_Man.ToString();
                this.ddl_WPlan_Number.SelectedValue = SModel.WPlan_Number.ToString();
                this.ddl_WPlan_Unit.SelectedValue = SModel.WPlan_Unit.ToString();
                if (SModel.WPlan_State == 1)
                {
                    this.cb_WPlan_State.Checked = true;
                }
                if (SModel.WPlan_Enable == 1)
                {
                    this.cb_WPlan_Enable.Checked = true;
                }
                else
                {
                    this.cb_WPlan_Enable.Checked = false;
                    this.ddl_WPlan_Number.Hidden = true;
                    this.ddl_WPlan_Unit.Hidden = true;
                }
                this.dp_WPlan_EndTime.Text = SModel.WPlan_StartTime.ToString("yyyy-MM-dd");
                this.dp_WPlan_StartTime.Text = SModel.WPlan_StartTime.ToString("yyyy-MM-dd");
               
            }
            else
            {
                this.dp_WPlan_StartTime.Text = System.DateTime.Now.ToString("yyyy-MM-dd");
                this.dp_WPlan_EndTime.Text = System.DateTime.Now.ToString("yyyy-MM-dd");
                this.ddl_WPlan_Man.SelectedValue = LinkService.GetCurrentUser().SUser_Id.ToString();
            }
        }
        public DateTime GetWaringDate(WorkPlan SM)
        {
            DateTime dtRes = System.DateTime.MaxValue;
            if (SM.WPlan_Unit == 2) //day
            {
                dtRes = SM.WPlan_StartTime.AddDays(-SM.WPlan_Number);
            }
            if (SM.WPlan_Unit == 3)//week
            {
                dtRes = SM.WPlan_StartTime.AddDays(-SM.WPlan_Number * 7);
            }
            if (SM.WPlan_Unit == 4)//month
            {
                dtRes = SM.WPlan_StartTime.AddMonths(-SM.WPlan_Number);
            }
            if (SM.WPlan_Unit == 5)
            {
                dtRes = SM.WPlan_StartTime.AddYears(-SM.WPlan_Number);
            }
            return dtRes;
        }
        #endregion

        #region Property
        WorkPlanOp Manager = new WorkPlanOp();
        new Sale_Model.WorkPlan SModel = new Sale_Model.WorkPlan();
        #endregion
    }
}
